package edu.cmu.ece.ds.droidchat.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.controller.UserListBaseAdapter;
import edu.cmu.ece.ds.droidchat.model.ChatSessionInfo;
import edu.cmu.ece.ds.droidchat.model.UserInfo;
import edu.cmu.ece.ds.droidchat.util.Toolbox;
import edu.cmu.ece.ds.droidchat.widget.TransparentPanel;

public class InviteUserMenuLayout extends TransparentPanel {
    private ListView mListView;
    private TextView mTextView;
    private ChatSessionInfo chInfo;
    private RelativeLayout parentView;
    private Animation animShow;
    private Animation animHide;

    public InviteUserMenuLayout(RelativeLayout parentView,
            ChatSessionInfo chInfo) {
        super(parentView.getContext());
        this.chInfo = chInfo;
        this.parentView = parentView;
        init(parentView.getContext());
    }

    private void init(Context context) {

        animShow = AnimationUtils.loadAnimation(context, R.anim.popup_show);
        animHide = AnimationUtils.loadAnimation(context, R.anim.popup_hide);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        this.setLayoutParams(layoutParams);
        this.setOrientation(LinearLayout.VERTICAL);

        // textview
        addTextView(context);
        // listview
        addListView(context);
        this.setVisibility(View.GONE);
    }

    private void addTextView(Context context) {
        mTextView = new TextView(context);
        mTextView.setText(context.getText(R.string.invite_join_chat_session));
        mTextView.setTextSize(15);
        mTextView.setTypeface(null, Typeface.BOLD);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        textParams.setMargins(10, 5, 10, 5);
        this.addView(mTextView, textParams);
    }

    private void addListView(Context context) {
        mListView = new ListView(context);
        ArrayList<UserInfo> nearbyUserList = new ArrayList<UserInfo>(
                Toolbox.retrieveAllNeighborsNotInList(chInfo));
        UserListBaseAdapter userListBaseAdapter = new UserListBaseAdapter(
                context, nearbyUserList, UserListBaseAdapter.THEME_TRANSPARENT);
        mListView.setAdapter(userListBaseAdapter);
        mListView.setTextFilterEnabled(true);
        mListView.setDivider(null);
        mListView.setCacheColorHint(Color.TRANSPARENT);
        LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        listParams.setMargins(10, 10, 10, 10);
        this.addView(mListView, listParams);
    }

    public ListView getListView() {
        return mListView;
    }

    public void popup() {
        parentView.addView(this);
        this.setVisibility(View.VISIBLE);
        this.startAnimation(animShow);
    }

    public void dismiss() {
        this.startAnimation(animHide);
        this.setVisibility(View.GONE);
        parentView.removeView(this);
    }

}
